package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallAdMapper;
import org.linlinjava.litemall.db.dao.LitemallChannelMapper;
import org.linlinjava.litemall.db.domain.LitemallAd;
import org.linlinjava.litemall.db.domain.LitemallAdExample;
import org.linlinjava.litemall.db.domain.LitemallChannel;
import org.linlinjava.litemall.db.domain.LitemallChannelExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallChannelService {
    @Resource
    private LitemallChannelMapper channelMapper;

    public List<LitemallChannel> querySelective( Integer page, Integer limit, String sort, String order) {
        LitemallChannelExample example = new LitemallChannelExample();
        LitemallChannelExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return channelMapper.selectByExample(example);
    }

    public int updateById(LitemallChannel channel) {
        channel.setUpdateTime(LocalDateTime.now());
        return channelMapper.updateByPrimaryKeySelective(channel);
    }

    public void deleteById(Integer id) {
        channelMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallChannel channel) {
        channel.setAddTime(LocalDateTime.now());
        channel.setUpdateTime(LocalDateTime.now());
        channelMapper.insertSelective(channel);
    }

    public LitemallChannel findById(Integer id) {
        return channelMapper.selectByPrimaryKey(id);
    }
    public LitemallChannel findByCode(String code) {
        LitemallChannelExample example = new LitemallChannelExample();
        LitemallChannelExample.Criteria criteria = example.createCriteria();
        criteria.andChannelCodeEqualTo(code);
        return channelMapper.selectOneByExample(example);
    }
}
